package com.clss.commandhelper.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * @author Liuv
 * @date 2017/4/12
 */

public final class ApiClient {
    private static final String TAG = "ApiClient";

    public static ApiList apiList;

    public static ApiConfiguration configuration;

    private static long TIME_OUT = 1000 * 60;

    public static void init(ApiConfiguration apiConfiguration) {

        configuration = apiConfiguration;
        String apiHost = ApiConstants.sChooseApi(configuration.getDataSourceType());
        apiList = initApiService(apiHost, ApiList.class);
    }

    private static <T> T initApiService(String baseUrl, Class<T> clazz) {

        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
//        if (BuildConfig.DEBUG) {
//            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//            builder.addInterceptor(httpLoggingInterceptor).addNetworkInterceptor(new StethoInterceptor());
//        }
        OkHttpClient client = builder
                .connectTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .readTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        return retrofit.create(clazz);
    }

}
